package cn.hznc.controller.uploadfilestrategy;

import cn.hznc.constant.UrlConstant;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import java.util.stream.Stream;

/**
 * @Author：Administrator
 * @Date：2024/2/28 16:29
 */
@Slf4j
public class RemoveFileLocal implements RemoveFileStrategy{

    @Override
    public void removeFile(String filename) {
        String fileUrl = UrlConstant.FILE_URL + filename;
        Path path = Paths.get(fileUrl);
        try (Stream<Path> walk = Files.walk(path)) {
            walk.sorted(Comparator.reverseOrder())
                    .forEach(filePath ->{
                        try {
                            Files.delete(filePath);
                        } catch (IOException e) {
                            log.error("删除文件异常");
                        }
                    });
        } catch (IOException e) {
            log.error("删除文件异常");
        }
    }
}
